package com.lesso.lpq.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lpq.sunflower.domain.repository.EmployeeAttendanceRuleRepository;
import com.lesso.lpq.sunflower.infra.assembler.EmployeeAssembler;
import com.lesso.lpq.sunflower.infra.assembler.EmployeeAttendanceRuleAssembler;
import com.lesso.lpq.sunflower.infra.mapper.EmployeeAttendanceRuleMapper;
import com.lesso.lpq.sunflower.model.EmployeeAttendanceRule;
import com.lesso.lpq.sunflower.pojo.dto.EmployeeAttendanceRuleDTO;
import com.lesso.lpq.sunflower.pojo.dto.EmployeeAttendanceRuleQueryDTO;
import com.lesso.lpq.sunflower.pojo.vo.EmployeeAttendanceRuleVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName : EmployeeAttendanceRuleRepositoryImpl
 * @Description :
 * @Author : 罗培棋
 * @Date: 2025-07-05 17:15
 */
@Repository
public class EmployeeAttendanceRuleRepositoryImpl implements EmployeeAttendanceRuleRepository {
    private final EmployeeAttendanceRuleMapper employeeAttendanceRuleMapper;

    public EmployeeAttendanceRuleRepositoryImpl(EmployeeAttendanceRuleMapper employeeAttendanceRuleMapper) {
        this.employeeAttendanceRuleMapper = employeeAttendanceRuleMapper;
    }

    @Override
    public Long save(EmployeeAttendanceRule employeeAttendanceRule) {
        if (employeeAttendanceRule.getId() == null) {
            employeeAttendanceRuleMapper.insert(employeeAttendanceRule);
        } else {
            employeeAttendanceRuleMapper.updateById(employeeAttendanceRule);
        }
        return employeeAttendanceRule.getId();
    }

    @Override
    public Page<EmployeeAttendanceRuleVO> getPageList(EmployeeAttendanceRuleQueryDTO dto) {
        Page<EmployeeAttendanceRuleVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        EntityWrapper<EmployeeAttendanceRule> wrapper = new EntityWrapper<>();
        wrapper.eq("del_status", 0);

        if (dto.getEmployeeId() != null) {
            wrapper.eq("employee_id", dto.getEmployeeId());
        }
        if (dto.getRuleCode() != null && !dto.getRuleCode().isEmpty()) {
            wrapper.eq("rule_code", dto.getRuleCode());
        }

        List<EmployeeAttendanceRule> employeeAttendanceRules = employeeAttendanceRuleMapper.selectPage(page, wrapper);
        List<EmployeeAttendanceRuleVO> employeeAttendanceRuleVOs = EmployeeAttendanceRuleAssembler.INSTANCE.entityListToVOList(employeeAttendanceRules);
        page.setRecords(employeeAttendanceRuleVOs);
        return page;
    }

    @Override
    public Page<EmployeeAttendanceRuleVO> getCustomPageList(EmployeeAttendanceRuleQueryDTO dto) {
        Page<EmployeeAttendanceRuleVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        RowBounds rowBounds = new RowBounds((dto.getCurrent() - 1) * dto.getSize(), dto.getSize());
        List<EmployeeAttendanceRuleVO> employeeAttendanceRuleVOs = employeeAttendanceRuleMapper.selectPageList(rowBounds, dto);
        page.setRecords(employeeAttendanceRuleVOs);
        return page;
    }

    @Override
    public void removeById(Long id) {
        employeeAttendanceRuleMapper.deleteById(id);
    }

    @Override
    public void updateById(EmployeeAttendanceRuleDTO dto) {
        EmployeeAttendanceRule employeeAttendanceRule = EmployeeAttendanceRuleAssembler.INSTANCE.dtoToEntity(dto);
        employeeAttendanceRuleMapper.updateById(employeeAttendanceRule);
    }

    @Override
    public EmployeeAttendanceRuleVO getByEmployeeId(String employeeId) {
        EntityWrapper<EmployeeAttendanceRule> wrapper = new EntityWrapper<>();
        wrapper.eq("employee_id",employeeId);
        wrapper.eq("del_status",0);
        EmployeeAttendanceRule employeeAttendanceRule = employeeAttendanceRuleMapper.selectById(employeeId);
        if(employeeAttendanceRule ==null){
            return null;
        }
        return EmployeeAttendanceRuleAssembler.INSTANCE.entityToVO(employeeAttendanceRule);
    }
}
